package com.atguigu.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(urlPatterns = "/Servlet01CreateCookie")
public class Servlet01CreateCookie extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、创建 Cookie 对象
        Cookie cookie = new Cookie("username", "zhangsan");

        // 2、把 Cookie 对象放入响应对象
        response.addCookie(cookie);

        // 3、正常返回响应即可
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("Cookie已返回");
    }
}
